//variabile globale che serve per registrare l'elemento attuale nello slider
var selectedElement;
//array globale per ricordare le risposte
var giocoRisposte = new Array();

$(document).ready(function(){
  $("#slider-incontinenza-item").cycle({
    timeout:0,
    onPrevNextEvent: function(isNext, zeroBasedSlideIndex, slideElement){
      var pageid = $(slideElement).attr("pageid");
      var thumbPages = $(".slider-incontinenza-thumbs-page");
      
      for(var i = 0;i<thumbPages.length;i++){
        if($(thumbPages[i]).attr("pageid") == pageid){
          selectedElement = thumbPages[i];
          $(thumbPages[i]).find("img").css({"opacity":"1"});
        }
        else
          $(thumbPages[i]).find("img").css({"opacity":"0.4"});
      }
    }
  });
  $("#arrowright").click(function(){
    var thumbPages = $(".slider-incontinenza-thumbs-page");
    var index;
    for(var i = 0;i<thumbPages.length;i++){
      if(thumbPages[i] == selectedElement)
        index = $(thumbPages[i]).attr("pageid");
    }
    if(index == $(thumbPages[thumbPages.length-1]).attr("pageid"))
      return;
    else $("#slider-incontinenza-item").cycle("next");
  });
  $("#arrowleft").click(function(){
    var thumbPages = $(".slider-incontinenza-thumbs-page");
    var index;
    for(var i = 0;i<thumbPages.length;i++){
      if(thumbPages[i] == selectedElement)
        index = $(thumbPages[i]).attr("pageid");
    }
    if(index == "1")
      return;
    else $("#slider-incontinenza-item").cycle("prev");
  });
});

var first_page_test=true;
$(document).ready(function(){
  $("#incontinenza-urinaria-gioco-controlli-next").click(function(){
    if(first_page_test!=true) return;
    first_page_test = false;
    $(this).fadeOut(100,function(){
      $(this).html("Vai alla domanda successiva");
      $(this).fadeIn();
    });
  })
  $("#incontinenza-urinaria-gioco-left").cycle({
    timeout:0
  });
  $("#incontinenza-urinaria-gioco-controlli-next").click(function(){
    var pages = $(".incontinenza-urinaria-gioco-page");
    var actualElement;
    var i, result, k;
    for(i=0;i<pages.length;i++){
      if($(pages[i]).css("display") == "block"){
        actualElement = $(pages[i]).find("input").attr("name");
        k=i;
      }
    }
    var numberOfElements = pages.length-2, lastElement = false;
    
    if(k == numberOfElements)
      lastElement = true;
    var val = $('input[name='+actualElement+']:checked').val();
    if(val != undefined || k == 0)
      $("#incontinenza-urinaria-gioco-left").cycle("next");
    if(val == undefined && k != 0)
      alert("devi rispondere alla domanda");
    if(lastElement == true){
      result = checkResults();
      $("#incontinenza-urinaria-gioco-controlli > span").fadeOut();
      $("#risultato-"+result).fadeIn();
    }
    
  });
});

function checkResults(){
  var pages = $(".incontinenza-urinaria-gioco-page");
  var results = 0;
  var val;
  for(var i = 1; i<pages.length-1;i++){
    val = $('input[name='+$(pages[i]).find("input").attr("name")+']:checked').val();
    results = results + parseInt(val);
  }
  if(results <= 4)
    return "ultra-mini";
  if(results >=5 && results <= 7)
    return "mini";
  if(results >=8 && results <= 9)
    return "mini-long";
  if(results >=10 && results <= 13)
    return "normal";
  if(results >=14 && results <= 15)
    return "extra";
  if(results >=16 && results <= 18)
    return "super";
  return false;
}


$(document).ready(function(){
  $(".gotop").click(function(){
    $("html body").animate({
      "scrollTop":$("#incontinenza-urinaria").offset().top
    },500);
  });
  
});